package likou.offer;

import java.util.HashMap;
import java.util.Map;

/**
 * @Info:
 * @Author: 唐小尊
 * @Date: 2020/8/27  21:02
 */
public class _48 {
    public static void main(String[] args) {
        System.out.println(lengthOfLongestSubstring("aabaab!bb"));
    }

    public static int lengthOfLongestSubstring(String s) {
        if (s.length() < 2) {
            return s.length();
        }
        byte[] bytes = s.getBytes();
        Map<Byte, Integer> map = new HashMap<>();
        int low = 0;
        int high = 1;
        map.put(bytes[0], 0);
        int max = 1;
        while (high < s.length()) {
            boolean bool = true;
            if (!map.containsKey(bytes[high])) {
                map.put(bytes[high], high);
            } else{
                int nowMax = high - low;
                int lastIndex = map.get(bytes[high]);
                if (lastIndex<low){
                    nowMax++;
                }else {
                    low = lastIndex + 1;
                }
                max = Math.max(nowMax, max);
                map.put(bytes[high], high);
                bool = false;
            }
            if (bool && high == s.length() - 1) {
                int nowMax = high - low + 1;
                max = Math.max(nowMax, max);
            }
            high++;
        }
        return max;
    }
}
